package com.baoli.blzj.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baoli.blzj.bean.home.HomeTotalListBean;
import com.baoli.blzj.customviews.ProgressDialogFragment;
import com.baoli.blzj.utils.SizeUtil;
import com.bumptech.glide.Glide;
import com.baoli.blzj.Constants;
import com.baoli.blzj.MainActivity;
import com.baoli.blzj.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * 首页整体适配器 by John on 17/6/29.
 */

public class HomeAdapter extends BaseAdapter implements Constants{

    private Context mContext;
    private List<HomeTotalListBean> mSections;
    public SharedPreferences spn;
    public SharedPreferences.Editor editor;
    private ProgressDialogFragment pf;
    private int screenWidth = 0;
    private int screenHeight = 0;

    public HomeAdapter(Context context) {
        this.mContext = context;
        this.mSections = new ArrayList<>();
        spn = context.getSharedPreferences(SPN_BLZJ, MODE_PRIVATE);
        editor = spn.edit();
        DisplayMetrics metric = new DisplayMetrics();
        ((MainActivity)context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        this.screenWidth = metric.widthPixels;
        this.screenHeight = metric.heightPixels;
    }

    public void setList(List<HomeTotalListBean> hBean) {
        mSections.clear();
        append(hBean);
    }

    public void append(List<HomeTotalListBean> hBean) {
        mSections.addAll(hBean);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mSections.size();
    }

    @Override
    public Object getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_home, null);
            holder = new ViewHolder();
            holder.list_title = (TextView) convertView.findViewById(R.id.list_title);
            holder.list_info = (TextView) convertView.findViewById(R.id.list_info);
            holder.is_scimg = (ImageView) convertView.findViewById(R.id.is_scimg);
            holder.is_scword = (TextView) convertView.findViewById(R.id.is_scword);
            holder.is_sc = (LinearLayout) convertView.findViewById(R.id.is_sc);
            holder.home_icon = (ImageView) convertView.findViewById(R.id.home_icon);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.home_icon.getLayoutParams();
            linearParams.width = screenWidth;
            linearParams.height = screenWidth / 10 * 5 - SizeUtil.Px2Dp(mContext,60);
            holder.home_icon.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.list_title.setText(mSections.get(position).getStore_name());
        holder.list_info.setText(mSections.get(position).getStore_note());
        Glide.with(mContext)
                .load(mSections.get(position).getLogo_img())
                .animate(R.anim.anim_photo)
                .placeholder(R.drawable.ic_launcher)
                .error(R.drawable.ic_launcher)
                .crossFade()
                .into(holder.home_icon);

        if ("0".equals(mSections.get(position).getIs_sc())) {
            holder.is_scimg.setBackgroundResource(R.drawable.dislike_btn);
            holder.is_scword.setText("收藏");
        } else {
            holder.is_scimg.setBackgroundResource(R.drawable.like_btn);
            holder.is_scword.setText("已收藏");
        }

        holder.is_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (spn.getBoolean(SPN_IS_LOGIN, false)) {
                    //httpGetKeFu(position, HTTP_SHOPCOLLECTION + "?uid=" + AESEncryptor.decrypt(spn.getString(SPN_UID, "")) + "&shopid=" + mSections.get(position).getStore_id());
                } else {
                    /*Intent intent = new Intent();
                    intent.setClass(mContext, PersonalLoginActivity.class);
                    ((MainActivity)mContext).startActivityForResult(intent, 101);*/
                }
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* Intent intent = new Intent();
                intent.setClass(mContext, CommunitySupermarketDetailActivity.class);
                intent.putExtra("store_name", mSections.get(position).getStore_name());
                intent.putExtra("store_id", mSections.get(position).getStore_id());
                mContext.startActivity(intent);*/
            }
        });

        return convertView;
    }

    /*private void httpGetKeFu(final int position, String url) {

        pf = new ProgressDialogFragment();
        pf.show(((MainActivity)mContext).getFragmentManager(), "1");
        JsonObjectHeadersPostRequest request = new JsonObjectHeadersPostRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String recode, msg;
                try {
                    if (response.has("recode")) {
                        recode = response.getString("recode");
                        msg = response.getString("msg");
                        if (recode.equals("0")) {
                            if ("0".equals(mSections.get(position).getIs_sc())) {
                                mSections.get(position).setIs_sc("1");
                            } else {
                                mSections.get(position).setIs_sc("0");
                            }
                            CustomToast.showToast(mContext, msg, 1000);
                            pf.dismiss();
                            handler.sendEmptyMessage(1);
                        } else if (recode.equals("111111")) {
                            CancelPublicActivity.getInstance().deleteData(mContext, msg);
                        } else {
                            CustomToast.showToast(mContext, msg, 1000);
                        }
                    }
                } catch (Exception e) {
                    CustomToast.showToast(mContext, R.string.http_fail, 1000);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                CustomToast.showToast(mContext, R.string.http_fail, 1000);
            }
        });
        queue.add(request);
    }*/

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private class ViewHolder {
        TextView list_title,list_info;
        LinearLayout is_sc;
        TextView is_scword;
        ImageView home_icon,is_scimg;
    }
}
